package com.erp.warehouse.clouderp_warehouse.service.impl.outbound;

import com.erp.warehouse.clouderp_warehouse.mapper.outbound.OutboundOrderItemMapper;
import com.erp.warehouse.clouderp_warehouse.mapper.outbound.OutboundOrderMapper;
import com.erp.warehouse.clouderp_warehouse.pojo.outbound.OutboundOrder;
import com.erp.warehouse.clouderp_warehouse.pojo.outbound.OutboundOrderCreate;
import com.erp.warehouse.clouderp_warehouse.pojo.outbound.OutboundOrderItem;
import com.erp.warehouse.clouderp_warehouse.pojo.outbound.OutboundOrderModify;
import com.erp.warehouse.clouderp_warehouse.service.outbound.OutboundService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 2025/11/7 0:11
 * author: 向远洋
 */
@Service
@Transactional
public class OutboundServiceImpl implements OutboundService {
    @Autowired
    OutboundOrderMapper mapper;
    @Autowired
    OutboundOrderItemMapper itemMapper;
    @Override
    public PageInfo<OutboundOrder> getPageInfo(Map<String, Object> params) {
        int pageNum = Integer.parseInt(params.get("pageNum").toString());
        int pageSize = Integer.parseInt(params.get("pageSize").toString());
        PageHelper.startPage(pageNum, pageSize);
        List<OutboundOrder> outboundOrders = mapper.selectOutboundOrderList(params);
        //对数组进行完善
        for (OutboundOrder outboundOrder : outboundOrders) {
            outboundOrder.setCustomerName("xxx客户");
            outboundOrder.setAuditorName("yyy审核员");
            outboundOrder.setStaffName("zzz业务员");
        }
        return new PageInfo<>(outboundOrders);
    }

    @Override
    public void createWithItems(OutboundOrderCreate request) {
        // 1. 插入主单
        OutboundOrder order = request.getOutboundOrder();
        List<OutboundOrderItem> items = request.getOutboundOrderItemList();
        // 2. 设置外键：将明细关联到主单，同时根据明细数量、价格修改入库主表的总数量、总价格
        Integer quantity = 0;
        BigDecimal totalPrice = new BigDecimal("0");
        for (OutboundOrderItem item : items) {
            quantity += item.getQuantity();
            totalPrice = totalPrice.add(item.getTotalPrice());
        }
        // 3. 插入入库主表和批量插入明细
        order.setTotalQuantity(quantity);
        order.setTotalMoney(totalPrice);
        mapper.insert(order);
        for (OutboundOrderItem item : items) item.setOutboundId(order.getId()); // 关联主单id
        itemMapper.insertOutboundOrderItemList(items);


    }

    @Override
    public OutboundOrderCreate getOutboundOrderCreate(Long id) {
        OutboundOrder outboundOrder = mapper.selectOutboundOrderById(id);
        List<OutboundOrderItem> outboundOrderItems = itemMapper.selectOutboundOrderItemList(id);
        //更新明细的仓库名、商品名、库位名
        outboundOrderItems.forEach(i -> {
            i.setGoodsName("xxx商品");
            i.setWarehouseName("xxx仓库");
            i.setWarehouseLocationName("xxx库位");
        });
        return new OutboundOrderCreate(outboundOrder, outboundOrderItems);
    }

    @Override
    public void modifyWithItems(OutboundOrderModify outboundOrderModify) {
        //入库单每次修改明细需要更新的（总数量和总价格）
        int totalQuantity = 0;
        BigDecimal totalMoney = new BigDecimal("0");
        //执行删除操作
        int[] delArr = outboundOrderModify.getDelArr().stream().mapToInt(i -> i).toArray();
        if(delArr.length > 0) itemMapper.deleteOutboundOrderItemList(delArr);
        //修改操作
        List<OutboundOrderItem> modArr = outboundOrderModify.getModArr();
        if(!modArr.isEmpty()) {
            for (OutboundOrderItem outboundOrderItem : modArr) {
                itemMapper.updateOutboundOrderItem(outboundOrderItem);
                totalQuantity += outboundOrderItem.getQuantity();
                totalMoney = totalMoney.add(outboundOrderItem.getTotalPrice());
            }
        }
        //新增操作
        List<OutboundOrderItem> addArr = outboundOrderModify.getAddArr();
        OutboundOrder outboundOrder = outboundOrderModify.getOutboundOrder();
        if(!addArr.isEmpty()) {
            for (OutboundOrderItem outboundOrderItem : addArr) {
                outboundOrderItem.setOutboundId(outboundOrder.getId());
                totalQuantity += outboundOrderItem.getQuantity();
                totalMoney = totalMoney.add(outboundOrderItem.getTotalPrice());
            }
            itemMapper.insertOutboundOrderItemList(addArr);
        }
        //判断是否需要修改出库单
        boolean modified = outboundOrderModify.getOrderModified();
        if(modified || delArr.length > 0 || !modArr.isEmpty() || !addArr.isEmpty()) {
            //修改总数量和总金额
            outboundOrder.setTotalQuantity(totalQuantity);
            outboundOrder.setTotalMoney(totalMoney);
            mapper.update(outboundOrder);
        }

    }
}
